package com.atguigu.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet(name = "Servlet10RequestParam", urlPatterns = "/Servlet10RequestParam")
public class Servlet10RequestParam extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("userName");
        String userAge = req.getParameter("userAge");

        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write("userName = " + userName);
        resp.getWriter().write("<br/>");
        resp.getWriter().write("userAge = " + userAge);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("userName");
        String userAge = req.getParameter("userAge");

        String[] userHobbies = req.getParameterValues("userHobby");

        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write("userName = " + userName);
        resp.getWriter().write("<br/>");
        resp.getWriter().write("userAge = " + userAge);
        resp.getWriter().write("<br/>");

        for (int i = 0; userHobbies != null && userHobbies.length > 0 && i < userHobbies.length; i++) {
            resp.getWriter().write("userHobby = " + userHobbies[i] + "<br/>");
        }
    }
}
